package com.yzycoc.hamster.demo.time202506.time20250630;

import net.coobird.thumbnailator.Thumbnails;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间： 2025/6/30
 *
 * @version 1.0
 **/
public class ImageCompound {

    // 基础路径配置
    private static final String HTML_PATH = "D:\\code\\hamster-demo\\src\\main\\resources\\static\\20250630\\10.html";
    private static final String IMAGE_SAVE_PATH = "static/images/";
    private static final int START_FILE_NUMBER = 44000000;

    public static void main(String[] args) {
        try {
            // 1. 读取HTML文件
            File input = new File(HTML_PATH);
            Document doc = Jsoup.parse(input, "UTF-8");

            // 2. 提取表格数据
            List<TableRow> tableData = extractTableData(doc);

            // 3. 下载并处理图片
            processImages(tableData);

            // 4. 生成枚举类
            generateEnumClass(tableData);

            System.out.println("处理完成！共处理 " + tableData.size() + " 行数据");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static List<TableRow> extractTableData(Document doc) {
        List<TableRow> rows = new ArrayList<>();
        Elements tableRows = doc.select("table tbody tr");

        for (Element row : tableRows) {
            Elements cells = row.select("td");
            if (cells.size() >= 3) {
                String column1 = cells.get(0).text().trim();
                String column2 = cells.get(1).select("img").attr("alt").trim();
                String column3 = cells.get(2).text().trim();
                String imgUrl = cells.get(1).select("img").attr("src");

                rows.add(new TableRow(column1, column2, column3, imgUrl));
            }
        }
        return rows;
    }

    private static void processImages(List<TableRow> tableData) throws IOException {
        // 创建图片保存目录
        new File(IMAGE_SAVE_PATH).mkdirs();

        int fileNumber = START_FILE_NUMBER;
        for (TableRow row : tableData) {
            try {
                String imageUrl = row.getImgUrl();
                if (!imageUrl.isEmpty()) {
                    String filePath = IMAGE_SAVE_PATH + fileNumber + ".png";

                    // 下载并转换图片
                    BufferedImage image = ImageIO.read(new URL(imageUrl));
                    Thumbnails.of(image)
                            .scale(1.0)
                            .outputFormat("png")
                            .toFile(new File(filePath));

                    row.setFileNumber(fileNumber);
                    fileNumber++;
                }
            } catch (IOException e) {
                System.err.println("无法下载图片: " + row.getImgUrl());
                e.printStackTrace();
            }
        }
    }

    private static void generateEnumClass(List<TableRow> tableData) {
        StringBuilder enumBuilder = new StringBuilder();
        enumBuilder.append("public enum TrophyLeague {\n");

        for (TableRow row : tableData) {
            if (row.getFileNumber() > 0) {
                String enumName = row.getColumn1().replaceAll("[^a-zA-Z0-9]", "_").toUpperCase();
                String description = row.getColumn2();

                enumBuilder.append(String.format(
                        "    %s(%d, \"%s\", \"%s\"),\n",
                        enumName,
                        row.getFileNumber(),
                        description,
                        row.getColumn3()
                ));
            }
        }

        // 移除最后一个逗号
        if (enumBuilder.length() > 0) {
            enumBuilder.setLength(enumBuilder.length() - 2);
        }

        enumBuilder.append(";\n\n");
        enumBuilder.append("    private final int fileNumber;\n");
        enumBuilder.append("    private final String description;\n");
        enumBuilder.append("    private final String trophyRange;\n\n");
        enumBuilder.append("    TrophyLeague(int fileNumber, String description, String trophyRange) {\n");
        enumBuilder.append("        this.fileNumber = fileNumber;\n");
        enumBuilder.append("        this.description = description;\n");
        enumBuilder.append("        this.trophyRange = trophyRange;\n");
        enumBuilder.append("    }\n\n");
        enumBuilder.append("    // Getters...\n");
        enumBuilder.append("}");

        System.out.println("生成的枚举类:\n");
        System.out.println(enumBuilder.toString());
    }

    static class TableRow {
        private final String column1;
        private final String column2;
        private final String column3;
        private final String imgUrl;
        private int fileNumber;

        public TableRow(String column1, String column2, String column3, String imgUrl) {
            this.column1 = column1;
            this.column2 = column2;
            this.column3 = column3;
            this.imgUrl = imgUrl;
        }

        // Getters and Setters...
        public String getColumn1() { return column1; }
        public String getColumn2() { return column2; }
        public String getColumn3() { return column3; }
        public String getImgUrl() { return imgUrl; }
        public int getFileNumber() { return fileNumber; }
        public void setFileNumber(int fileNumber) { this.fileNumber = fileNumber; }
    }
}
